class Solution
{
public:
    long long minimumCost(string s)
    {
        int n = s.size();
        long long zeroCost = 0;
        long long oneCost = 0;
        bool toZeroFlag = false;
        bool toOneFlag = false;

        auto _xor = [](bool a, bool b) -> bool
        {
            return (a && !b) || (!a && b);
        };

        int middle = s.size() / 2;

        for (int i = middle - 1; i >= 0; --i)
        {
            int current = s[i] - '0';
            int zeroStatus = _xor(current, toZeroFlag);
            if (zeroStatus != 0)
            {
                toZeroFlag = !toZeroFlag;
                zeroCost += i + 1;
            }
            int oneStatus = _xor(current, toOneFlag);
            if (oneStatus != 1)
            {
                toOneFlag = !toOneFlag;
                oneCost += i + 1;
            }
        }

        toZeroFlag = false;
        toOneFlag = false;
        for (int i = middle; i < n; ++i)
        {
            int current = s[i] - '0';
            int zeroStatus = _xor(current, toZeroFlag);
            if (zeroStatus != 0)
            {
                toZeroFlag = !toZeroFlag;
                zeroCost += n - i;
            }
            int oneStatus = _xor(current, toOneFlag);
            if (oneStatus != 1)
            {
                toOneFlag = !toOneFlag;
                oneCost += n - i;
            }
        }

        return min(zeroCost, oneCost);
    }
};